package demo.jvm.gc.reference;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * -verbose:gc -Xms4m -Xmx4m -Xmn2m
 * -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:./gclogs
 * GCViewer工具
 * GCeasy日志分析工具
 * @author taqo
 * @date 2021/5/12
 */
public class TestA {
    static class OOMClass{
        private int[] oom = new int[1024*100];//100k
    }
    public static void main(String[] args) throws InterruptedException {
        m1();
        // m2();
    }

    // 触发oom
    private static void m2() throws InterruptedException {
        List<OOMClass> list = new ArrayList<>();
        while (true){
            for (int i = 0; i < 100; i++) {
                list.add(new OOMClass());
            }
            Thread.sleep(500);
        }
    }

    // 可持续跑
    private static void m1() throws InterruptedException {
        ReferenceQueue<OOMClass> queue = new ReferenceQueue<>();
        List<SoftReference> list = new ArrayList<>();
        while (true){
            for (int i = 0; i < 100; i++) {
                list.add(new SoftReference<OOMClass>(new OOMClass(),queue));
            }
            Thread.sleep(500);
            System.out.println("==============>:"+list.size());
        }
    }
}
